import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttermodule/config/iconfont.dart';
import 'package:fluttermodule/customerInfo/customer_info_model.dart';
import 'package:fluttermodule/provide/customer_info_provide.dart';
import 'package:provide/provide.dart';

class CustomerInfoPage extends StatefulWidget {
  CustomerInfoPage();


  @override
  _CustomerInfoPageState createState() {
    return _CustomerInfoPageState();
  }
}

class _CustomerInfoPageState extends State<CustomerInfoPage> {
  _CustomerInfoPageState();

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    return Scaffold(
       appBar: new AppBar(
         title: Text('顾客信息',
           style: TextStyle(
             color: const Color(0xFF000000)
           ),
         ),
         elevation: 0,
         backgroundColor: const Color(0xFFFFFFFF),
         centerTitle:true,
         leading: IconButton(
           icon: Icon(Icons.arrow_back_ios,
             color: const Color(0xFF000000)),
            onPressed: (){ Navigator.pop(context);}
         ),

       ),
      body: FutureBuilder(
        future: _getInfo(context),
        builder: (context,snapshot){
          return Page();
        }
      ),
    );
    }
  }

  class Page extends StatelessWidget{

    @override
    Widget build(BuildContext context) {
      CustomInfo customerInfo = Provide.value<CustomerInfoProvide>(context).customerInfo;
      bool hasData;
      if(customerInfo == null){
        customerInfo = new CustomInfo();
        hasData = false;
      }else{
        hasData = true;
      }
      return Stack(
        children: <Widget>[
          Container(
            color: const Color(0xFFFFFFFF),
            child: CustomScrollView(
              shrinkWrap: true,
              // 内容
              slivers: <Widget>[
                new SliverList(
                  delegate: new SliverChildListDelegate(
                    <Widget>[
                      Center(child: new ClipOval(
                        child: Container(
                          child: Center(child:Text(customerInfo.nameLetter, style: TextStyle(
                              color: const Color(0xFFFFFFFF),
                              fontSize:30
                          ),)),
                          color: const Color(0xFFA6A6A6),
                          width: 70,
                          height: 70,
                        ),
                      )),

                      InfoItem(title:"顾客姓名",value:customerInfo.name),
                      InfoItem(title:"安利卡号",value:customerInfo.cardNo),
                      InfoItem(title:"到期日",value:customerInfo.validate),
                      Container(color: Color(0xFFF4F4F4),
                        height: 10,
                        width: 1334,),
                      InfoItem(title:"顾客分组",value:customerInfo.group),
                      InfoItem(title:"手机号",value:customerInfo.phoneNo),
                      InfoItem(title:"介绍人",value:customerInfo.introducer),
                      InfoItem(title:"优惠券",value:customerInfo.couponInfo.name + "  "+ customerInfo.couponInfo.validate),
                      WidgetItem(title:"标签",valueChild:
                      new Container(
                          constraints: BoxConstraints.tightFor(width:ScreenUtil().setWidth(500),height:ScreenUtil().setWidth(96)),
                          child: new Row(
                              children: <Widget>[
                                Flexible(
                                  child: ListView.builder(
                                      scrollDirection: Axis.horizontal,
                                      itemCount: customerInfo.tagList == null? 0:customerInfo.tagList.length,
                                      shrinkWrap: true,
                                      itemBuilder: (BuildContext context,int index){
                                        return TagItem(value: customerInfo.tagList.elementAt(index));
                                      }
                                  ),
                                  flex: 1,
                                )
                              ])

                      )
                      ),
                      Container(color: Color(0xFFF4F4F4),
                        height: 10,
                        width: 1334,),
                      TitleItem(icon:IconFont.icon_info,title: "安利信息"),
                      InfoItem(title: "加入时间",value: customerInfo.amwayInfo.joinTime, color: 0xFFF6F6F6,),
                      TitleItem(icon:IconFont.icon_list,title: "身份信息"),
                      InfoItem(title: "籍贯",value: customerInfo.personalInfo.nativePlace, color: 0xFFF6F6F6,),
                      InfoItem(title: "学历",value: customerInfo.personalInfo.grade, color: 0xFFF6F6F6,),
                      InfoItem(title: "职业",value: customerInfo.personalInfo.job, color: 0xFFF6F6F6,),
                      InfoItem(title: "收入",value: customerInfo.personalInfo.income, color: 0xFFF6F6F6,),
                      TitleItem(icon:IconFont.icon_other,title: "备注"),
                      InfoItem(title: "安利雅姿",value: customerInfo.otherInfo.anmay, color: 0xFFF6F6F6,),
                    ],
                  ),
                ),
              ],
            )
          ),
          Opacity(
            opacity: hasData ? 0.0 : 1.0,
            child: Center(child: CircularProgressIndicator()),
          )
        ],

      );
    }
  }


class WidgetItem extends StatelessWidget{
  String title;
  Widget valueChild;

  WidgetItem({this.title, this.valueChild});

  @override
  Widget build(BuildContext context) {
    return Row(
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.max,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          new Container(
              padding:EdgeInsets.fromLTRB(25, 12, 0, 12),
              width: 90,
              child:new Text(
                title+ ":",
                style: new TextStyle(fontSize:14,
                  color: const Color(0xFFA6A6A6),),
              )
          ),
          new Padding(padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
              child: valueChild,
          ),
        ],

    );
  }
}

class InfoItem extends StatelessWidget{
  String title;
  String value;
  int color;

  InfoItem({this.title, this.value,this.color});

  @override
  Widget build(BuildContext context) {
    if(color == null){
      color = 0xFFFFFFFF;
    }
    return Container(
        color: Color(color),
        child:WidgetItem(title:title,valueChild:
            Container(
              padding: EdgeInsets.fromLTRB(0, 12, 0, 12),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                      child: Text(value,
                      maxLines: 2,
                      style: TextStyle(
                          fontSize: 14,
                          decoration: TextDecoration.none),
                      overflow: TextOverflow.ellipsis,
                    ))
                ],
              ),
              constraints: BoxConstraints.loose(Size(ScreenUtil().setWidth(500), ScreenUtil().setHeight(130))),
            )
        )
    );
  }
}

class TitleItem extends StatelessWidget{
  IconData icon;
  String title;

  TitleItem({this.icon, this.title});

  @override
  Widget build(BuildContext context) {
    return new Column(children: <Widget>[
      new Row(

      mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.max,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            new Padding(
              padding:EdgeInsets.fromLTRB(25, 10, 0, 10),
              child: Icon(icon,size: ScreenUtil().setSp(38),color:const Color(0xFF0C71DA)),
            )
            ,
            new Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                child: new Text(
                    title,
                    style: new TextStyle(fontSize:16,
                      color: const Color(0xFF0C71DA),
                    )))
          ]
      ),
      Container(color: Color(0xFFE2E2E2),
        height: 3,
        width: 1334,),
    ],);
  }
}


class TagItem extends StatelessWidget{
  String value;

  TagItem({this.value});

  @override
  Widget build(BuildContext context) {
    return new Container(
      padding:EdgeInsets.fromLTRB(0, 10, 15, 10),
      width: 80,
      child: ClipRRect(
        borderRadius: BorderRadius.all(Radius.circular(14)),
        child: Container(
          color: Color(0xF5F5F5F5),
          height: 20,
          child: Center(
            child: Text(
              value,
              style: TextStyle(
                  color: Color(0xFF888888),
                  fontSize: 12
              ),
            ),
          ),
        ),
      ),
    );
  }
}



Future _getInfo(BuildContext context) async{
  try {
    await Provide.value<CustomerInfoProvide>(context).getCustomerInfo("111");
  } catch (e) {
    print(e);
  }
}
